var date1 = new Date('2019-10-07')
var date2 = new Date()

var s1 = date1.getTime(),
  s2 = date2.getTime();

var total = (s2 - s1) / 1000;

var day = parseInt(total / (24 * 60 * 60)); //计算整数天数


const getTrueNumber = x => (x < 0 ? Math.abs(x) : x);
// const comingdate = new Date("2019-10-07");

const d = document.getElementById("d");

d.innerHTML = getTrueNumber(day);





var date1 = new Date('2019-09-20')
var date2 = new Date()

var s1 = date1.getTime(),
  s2 = date2.getTime();

var total = (s2 - s1) / 1000;

var day = parseInt(total / (24 * 60 * 60)); //计算整数天数

h.innerHTML = getTrueNumber(day);


// 计算农历

let lunar = {
  tg: '甲乙丙丁戊己庚辛壬癸',
  dz: '子丑寅卯辰巳午未申酉戌亥',
  number: '一二三四五六七八九十',
  year: '鼠牛虎兔龙蛇马羊猴鸡狗猪',
  month: '正二三四五六七八九十冬腊',
  monthadd: [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334],
  calendar: [0xA4B, 0x5164B, 0x6A5, 0x6D4, 0x415B5, 0x2B6, 0x957, 0x2092F, 0x497, 0x60C96, 0xD4A, 0xEA5, 0x50DA9, 0x5AD, 0x2B6, 0x3126E, 0x92E, 0x7192D, 0xC95, 0xD4A, 0x61B4A, 0xB55, 0x56A, 0x4155B, 0x25D, 0x92D, 0x2192B, 0xA95, 0x71695, 0x6CA, 0xB55, 0x50AB5, 0x4DA, 0xA5B, 0x30A57, 0x52B, 0x8152A, 0xE95, 0x6AA, 0x615AA, 0xAB5, 0x4B6, 0x414AE, 0xA57, 0x526, 0x31D26, 0xD95, 0x70B55, 0x56A, 0x96D, 0x5095D, 0x4AD, 0xA4D, 0x41A4D, 0xD25, 0x81AA5, 0xB54, 0xB6A, 0x612DA, 0x95B, 0x49B, 0x41497, 0xA4B, 0xA164B, 0x6A5, 0x6D4, 0x615B4, 0xAB6, 0x957, 0x5092F, 0x497, 0x64B, 0x30D4A, 0xEA5, 0x80D65, 0x5AC, 0xAB6, 0x5126D, 0x92E, 0xC96, 0x41A95, 0xD4A, 0xDA5, 0x20B55, 0x56A, 0x7155B, 0x25D, 0x92D, 0x5192B, 0xA95, 0xB4A, 0x416AA, 0xAD5, 0x90AB5, 0x4BA, 0xA5B, 0x60A57, 0x52B, 0xA93, 0x40E95],
};
// 获取阴历日期
const getLunarDate = (date) => {
  let useDate = new Date();
  let year,
    month,
    day;
  if (!date) {
    date = new Date(), year = date.getFullYear(), month = date.getMonth(), day = date.getDate();
  } else {
    date = date.split('-'), year = parseInt(date[0]), month = date[1] - 1, day = parseInt(date[2]);
  }

  if (year < 1921 || year > 2100) {
    return {};
  }

  let total,
    m,
    n,
    k,
    bit,
    lunarYear,
    lunarMonth,
    lunarDay;
  let isEnd = false;
  let tmp = year;
  if (tmp < 1900) {
    tmp += 1900;
  }
  total = (tmp - 1921) * 365 + Math.floor((tmp - 1921) / 4) + lunar.monthadd[month] + day - 38;
  if (year % 4 == 0 && month > 1) {
    total++;
  }
  for (m = 0;; m++) {
    k = (lunar.calendar[m] < 0xfff) ? 11 : 12;
    for (n = k; n >= 0; n--) {
      bit = (lunar.calendar[m] >> n) & 1;
      if (total <= 29 + bit) {
        isEnd = true;
        break;
      }
      total = total - 29 - bit;
    }
    if (isEnd) break;
  }
  lunarYear = 1921 + m;
  lunarMonth = k - n + 1;
  lunarDay = total;
  if (k == 12) {
    if (lunarMonth == Math.floor(lunar.calendar[m] / 0x10000) + 1) {
      lunarMonth = 1 - lunarMonth;
    }
    if (lunarMonth > Math.floor(lunar.calendar[m] / 0x10000) + 1) {
      lunarMonth--;
    }
  }

  return {
    lunarYear,
    lunarMonth,
    lunarDay,
  };
};

// 获取阴历年信息
const getLunarDateString = (lunarDate) => {
  if (!lunarDate.lunarDay) return;
  let data = {};
  let lunarYear = lunarDate.lunarYear;
  let lunarMonth = lunarDate.lunarMonth;
  let lunarDay = lunarDate.lunarDay;

  data.tg = lunar.tg.charAt((lunarYear - 4) % 10);
  data.dz = lunar.dz.charAt((lunarYear - 4) % 12);
  data.year = lunar.year.charAt((lunarYear - 4) % 12);
  data.month = lunarMonth < 1 ? `(闰)${lunar.month.charAt(-lunarMonth - 1)}` : lunar.month.charAt(lunarMonth - 1);

  data.day = (lunarDay < 11) ? '初' : ((lunarDay < 20) ? '十' : ((lunarDay < 30) ? '廿' : '三十'));
  if (lunarDay % 10 != 0 || lunarDay == 10) {
    data.day += lunar.number.charAt((lunarDay - 1) % 10);
  }

  return data;
};

// const aaa = getLunarDate('2020-11-5')
// console.log(aaa)  // {lunarYear: 2020, lunarMonth: 9, lunarDay: 20}

// const bbb = getLunarDateString(aaa)
// console.log(bbb)  // {tg: "庚", dz: "子", year: "鼠", month: "九", day: "廿"}

// ① 当前时间
var newDate = new Date()
var year = newDate.getFullYear()
var month = newDate.getMonth() + 1
var date = newDate.getDate()
var today = year + '-' + month + '-' + date
var lunarDate = getLunarDate(today)
var newLunarDate = lunarDate.lunarYear + '-' + lunarDate.lunarMonth + '-' + lunarDate.lunarDay

var brithday = new Date().getFullYear() + '-9-20'

// 减法
var subtraction = (new Date(newLunarDate).getTime() - new Date(brithday).getTime()) / 1000

var day = parseInt(subtraction / (24 * 60 * 60)); //计算整数天数

if (day < 0) {
  // 为负数时，则开始倒计时
  const m = document.getElementById("m");
  m.innerHTML = getTrueNumber(day);
} else if (day > 0) {
  // 为正数时，生日年份加1，然后重新计算倒计时
  var brithdayYear = parseInt(brithday.substring(0, 4)) + 1
  brithday = brithdayYear + '-9-20'
  // 减法
  var subtraction = (new Date(newLunarDate).getTime() - new Date(brithday).getTime()) / 1000

  var day = parseInt(subtraction / (24 * 60 * 60)); //计算整数天数

  const m = document.getElementById("m");
  m.innerHTML = getTrueNumber(day);
}


// 生日
function diffDate(date1, date2) {
  let begin = new Date(date1);
  let end = new Date(date2);

  // 如果开始时间晚于结束时间，交换日期，并记录交换状态
  let swap = false;
  if (begin > end) {
    let tmp = begin;
    begin = end;
    end = tmp;
    swap = true;
  }

  // 分别取二个日期的年、月、日值
  let d1 = begin.getDate();
  let m1 = begin.getMonth() + 1;
  let y1 = begin.getFullYear();
  let d2 = end.getDate();
  let m2 = end.getMonth() + 1;
  let y2 = end.getFullYear();

  // 获取每个月的天数，这里要注意一下闰年的2月
  var getMonthDays = function (y, m) {
    var aMonthDays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    if (m == 2 && ((y % 400 == 0) || (y % 4 == 0 && y % 100 != 0))) {
      return 29;
    }
    return aMonthDays[m];
  };

  let y, m, d
  let tempD = 0
  let tmpM = 0
  let tmpY = 0

  // 计算日，不足时向月份借
  if (d2 >= d1) {
    d = d2 - d1
  } else {
    tmpM = -1
    d = getMonthDays(y1, m1) + d2 - d1
  }

  // 计算月，不足时向年份借
  if (m2 + tmpM >= m1) {
    m = m2 + tmpM - m1
  } else {
    tmpY = -1
    m = 12 + m2 + tmpM - m1
  }

  // 计算年
  y = y2 + tmpY - y1

  // 拼接距离字符串 输出格式如："5天"， "5月 05天"， "5年 05月 05日"
  let str = "";
  if (y > 0) {
    str = y + '年 ' + ("0" + m).substr(-2) + "月 " + ("0" + d).substr(-2) + '天 '
  } else if (m > 0) {
    str = m + "月 " + ("0" + d).substr(-2) + '天 '
  } else if (d > 0) {
    str = d + '天 '
  }

  // 完整输出
  // return date1 + " 比 " + date2 + (swap ? " 晚 " : " 早 ") + str;

  // 输出年份
  return y
}

var age = diffDate(newLunarDate, "1996-09-20")

const s = document.getElementById("s");
s.innerHTML = getTrueNumber(age);


// var date3 = new Date()
// var tYear = date3.getFullYear();
// console.log(tYear)

// var tMonth = date3.getMonth();

//   var m = tMonth + 1;
//   // if (m.toString().length == 1) {
//   //     m = "0" + m;
//   // }
// console.log(m)




// const h = document.getElementById("h");
// const m = document.getElementById("m");
// const s = document.getElementById("s");

// const countdown = setInterval(() => {
//   const now   = new Date();
//   const des   = comingdate.getTime() - now.getTime();
//   const days  = Math.floor(des / (1000 * 60 * 60 * 24));
//   const hours = Math.floor((des % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
//   const mins  = Math.floor((des % (1000 * 60 * 60)) / (1000 * 60));
//   const secs  = Math.floor((des % (1000 * 60)) / 1000);

// d.innerHTML = getTrueNumber(day);
//   h.innerHTML = getTrueNumber(hours);
//   m.innerHTML = getTrueNumber(mins);
//   s.innerHTML = getTrueNumber(secs);

//   if (x <= 0) clearInterval(x);
// }, 1000);